package de.uni_potsdam.hpi.bpt.ai.example;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONException;

import de.uni_potsdam.hpi.bpt.ai.collection.Model;
import de.uni_potsdam.hpi.bpt.ai.diagram.Diagram;
import de.uni_potsdam.hpi.bpt.ai.diagram.Shape;
import de.uni_potsdam.hpi.bpt.ai.util.BpmMetrics;

public class CountModelsWithProperty extends Experiment {

	//Map<String, Map<String, Integer>> count = new HashMap<String, Map<String, Integer>>();

	/*
	@Override
	protected void examineModel(Model model, Diagram diagram, int index) {
		String ns = diagram.getStencilset().getNamespace();
		
		if (!this.count.containsKey(ns)) {
			this.count.put(ns, new HashMap<String, Integer>());
		}

		Map<String, Integer> current = this.count.get(ns);
//		
//		// cycles
//		if (!current.containsKey(CYC)) {
//			current.put(CYC, 0);
//		}
//		if (new TransitiveClosure(diagram).hasLoops()) {
//			current.put(CYC, current.get(CYC)+1);
//		}
//		
//		// concurrency
//		if (!current.containsKey(TS)) {
//			current.put(TS, 0);
//		}
//		if (BpmMetrics.ts(diagram) > 0) {
//			current.put(TS, current.get(TS)+1);
//		}
		
		
//		for (Shape s : diagram.getShapes()) {
//			if (s instanceof Node) {
//				System.out.println("node" + s.getStencilId());
//			}
//			else {
//				System.out.println(s.getStencilId());
//			}
//		}
		
		for (String sse: diagram.getSsextensions()) {
			if (!current.containsKey(sse)) {
				current.put(sse, 0);
			}
			current.put(sse, current.get(sse)+1);
		}
		
		String POOL = "Pool";
		
		if (!current.containsKey(POOL)) {
			current.put(POOL, 0);
		}
		
		int pools = 0;
		for (Shape s : diagram.getShapes()) {
			if (POOL.equals(s.getStencilId())) {
				pools++;
			}
		}
		if (pools > 1) {
			current.put(POOL, current.get(POOL) + 1);
		}
		
	}
	
	
	protected void printResults(){
		for (String ns : this.count.keySet()) {
			System.out.println(ns);
			for (Entry<String, Integer> data : this.count.get(ns).entrySet()) {
				System.out.println(data.getKey()+";"+data.getValue());
			}
		}
	}
*/	
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, JSONException, IOException {
		CountModelsWithProperty ex = new CountModelsWithProperty();
		ex.eachModel(
			new HashSet<String>(Arrays.asList(new String[]{
				NS_EPC,
				//NS_BPMN
			}))
		);
		
		ex.printResults();
		
	}

	Map<Double, Integer> data = new HashMap<Double, Integer>();
	
	@Override
	protected void examineModel(Model model, Diagram diagram, int index) {
		double num = BpmMetrics.maxDc(diagram);
		if (!data.containsKey(num)) {
			data.put(num, 0);
		}
		data.put(num, data.get(num)+1);
	}
	
	protected int getNumberOfChildShapese(Shape shape) {
		int num = shape.getChildShapes().size();
		for (Shape child : shape.getChildShapes()) {
			num += getNumberOfChildShapese(child);
		}
		return num;
	}
	
	protected void printResults() {
		List<Double> list = Arrays.asList(data.keySet().toArray(new Double[data.keySet().size()]));
		Collections.sort(list);
		
		for (double i : list) {
			System.out.println(i+";"+data.get(i));
		}
	}

}
